﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace YXShop.Web.admin.systeminfo
{
    public partial class shop_setting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitWebControl();
            if(!this.Page.IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("010001008");
                GetModel();
            }
        }
        private void Save()
        {
           
            YXShop.BLL.SystemInfo.ShopSetting bll = new YXShop.BLL.SystemInfo.ShopSetting();
            YXShop.Model.SystemInfo.ShopSetting model = new YXShop.Model.SystemInfo.ShopSetting();
            try
            {
                if (this.txtTel.Text!="")
                {
                    if(!ChangeHope.Common.ValidateHelper.IsPhone(txtTel.Text))
                    {
                        this.ltlMsg.Text = "操作失败,请输入正确电话号码！";
                        this.pnlMsg.CssClass = "actionErr";
                        this.pnlMsg.Visible = true;
                        return;
                    }
                }
                if (this.txtZip.Text!="")
                {
                    if (!ChangeHope.Common.ValidateHelper.IsSend(txtZip.Text))
                    {
                        this.ltlMsg.Text = "操作失败,请输入正确邮政编码！";
                        this.pnlMsg.CssClass = "actionErr";
                        this.pnlMsg.Visible = true;
                        return;
                    }
                }
                model.ShopId = ChangeHope.Common.StringHelper.StringToInt(this.txtShopId.Value);
                model.Adress = this.txtAdress.Text;
                model.NumberLimit = ChangeHope.Common.StringHelper.StringToInt(this.txtNumberLimit.Text);
                model.OrdersText = this.txtOrdersText.Text;
                model.Tel = this.txtTel.Text;
                model.Zip = this.txtZip.Text;

                model.AllowVisitorBuy = ChangeHope.Common.StringHelper.StringToInt(this.rbtAllowVisitorBuy.SelectedValue);
                model.Coupon = ChangeHope.Common.StringHelper.StringToInt(this.rbtCoupon.SelectedValue);
                model.OrdersReceive = ChangeHope.Common.StringHelper.StringToInt(this.rbtOrdersReceive.SelectedValue);
                model.Thumbnails = ChangeHope.Common.StringHelper.StringToInt(this.rbtThumbnails.SelectedValue);
                model.WaterMark = ChangeHope.Common.StringHelper.StringToInt(this.rbtWaterMark.SelectedValue);
                model.AllowAuctionDeposit= ChangeHope.Common.StringHelper.StringToInt(this.rblAllowAuctionDeposit.SelectedValue);
                model.AllowGroupbuyDeposit = ChangeHope.Common.StringHelper.StringToInt(this.rbtAllowGroupBuyDeposit.SelectedValue);
                bll.AddOrUpdate(model);
                this.ltlMsg.Text = "保存成功!<br/>";
                this.pnlMsg.CssClass = "actionOk";
                this.pnlMsg.Visible = true;

            }
            catch (Exception ex) {

                this.ltlMsg.Text = "操作失败<br/>"+ex.ToString();
                this.pnlMsg.CssClass = "actionErr";
                this.pnlMsg.Visible = true;
            }
            finally
            {
                ChangeHope.Common.CacheClass.RemoveOneCache("ShopSetting");
                model = null;
                bll = null;
            }
        }
        private void GetModel()
        {
            YXShop.BLL.SystemInfo.ShopSetting bll = new YXShop.BLL.SystemInfo.ShopSetting();
            YXShop.Model.SystemInfo.ShopSetting model = bll.GetModel();
            if(model!=null)
            {
                this.txtShopId.Value = model.ShopId.ToString();
                this.txtAdress.Text = model.Adress;
                this.txtNumberLimit.Text = model.NumberLimit.ToString();
                this.txtOrdersText.Text = model.OrdersText;
                this.txtTel.Text = model.Tel;
                this.txtZip.Text = model.Zip;

                this.rbtAllowVisitorBuy.SelectedValue = model.AllowVisitorBuy.ToString();
                this.rbtCoupon.SelectedValue = model.Coupon.ToString();
                this.rbtOrdersReceive.SelectedValue = model.OrdersReceive.ToString();
                this.rbtThumbnails.SelectedValue = model.Thumbnails.ToString();
                this.rbtWaterMark.SelectedValue = model.WaterMark.ToString();
                this.rbtAllowGroupBuyDeposit.SelectedValue = model.AllowGroupbuyDeposit.ToString();
                this.rblAllowAuctionDeposit.SelectedValue = model.AllowAuctionDeposit.ToString();
            }
            bll = null;
            model = null;
        }

        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtNumberLimit,"请输入同一种商品的订单限制，0为不限制","isint","必填","该项必须是数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtAdress, "请输入您的详细地址，以方便用户能顺利联系到您", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtOrdersText, "订单的前缀", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtTel, "请输入您的电话", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtZip, "请输入您所在地的邮政编码", "isint_6_6", "必填", "该项为必填项[必须为6位数字]");
            this.Form.Attributes.Add("onsubmit", "return CheckForm();");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            YXShop.Common.PromptInfo.Popedom("010001012", "对不起，您没有权限进行修改");
            Save();
        }
    }
}
